// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode 

package anyVehicle;

import java.nio.FloatBuffer;

import javax.vecmath.Quat4f;

import net.minecraft.src.Block;
import net.minecraft.src.Entity;
import net.minecraft.src.MathHelper;
import net.minecraft.src.ModLoader;
import net.minecraft.src.Render;
import net.minecraft.src.RenderBlocks;
import net.minecraft.src.RenderHelper;
import net.minecraft.src.Tessellator;
import net.minecraft.src.TileEntity;
import net.minecraft.src.TileEntityRenderer;
import net.minecraft.src.Vec3D;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.MatrixUtil;
import com.bulletphysics.linearmath.Transform;

// Referenced classes of package net.minecraft.src:
//            Render, ModelAnyVehicle, EntityAnyVehicle, MathHelper, 
//            ModelBase, Entity

public class RenderVehicle extends Render {
	private static float[] glMatrix = new float[16];
	private static FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(16);
	
	public RenderVehicle() {
		super();
		shadowSize = 0.5F;

	}

	public void doRender(Entity entity, double x, double y, double z, float angle, float f1) {
		Vehicle v = (Vehicle) entity;
		renderVehicle(v, x, y, z, angle, f1);
//		renderOneBlock(v,x,y,z);
	}

	private void renderOneBlock(Vehicle v, double x, double y, double z) {
		RenderBlocks rb = new RenderBlocks(v.vehicleBlocks);
		RenderHelper.disableStandardItemLighting();
		GL11.glPushMatrix();
		setupTranslationAndRotation(v, x, y, z);
		rb.renderBlockOnInventory(Block.stone, 0, v.getEntityBrightness(1f));
		GL11.glPopMatrix();
	}

	public void renderVehicle(Vehicle v, double x, double y, double z, float angle, float ticks) {
		RenderBlocks rb = new RenderBlocks(v.vehicleBlocks);
		RenderHelper.disableStandardItemLighting();
		int totalPasses = 1;
		int pass = 0;
		Vec3D corner1 = v.getLowerBackLeftCorner();
		Vec3D corner2 = v.getUpperFrontRightCorner(corner1);

		while (pass < totalPasses) {
			GL11.glPushMatrix();

			setupTranslationAndRotation(v, x, y, z);

			for (int i = (int) corner1.xCoord - 1; i <= corner2.xCoord; i++) {
				for (int j = (int) corner1.yCoord - 1; j <= corner2.yCoord; j++) {
					for (int k = (int) corner1.zCoord - 1; k <= corner2.zCoord; k++) {
						int blockID = v.vehicleBlocks.getBlockId(i, j, k);

						if (blockID != 0) {
							Block b = Block.blocksList[blockID];
							if (pass == 0) {
								TileEntity tileentity = v.vehicleBlocks.getBlockTileEntity(i, j, k);
								if (TileEntityRenderer.instance.hasSpecialRenderer(tileentity)) { 
									TileEntityRenderer.instance.renderTileEntityAt(tileentity, i, j, k, angle);
								}
								loadTexture("/terrain.png");
							}

							Tessellator.instance.startDrawingQuads();
							totalPasses = Math.max(b.getRenderBlockPass() + 1, totalPasses);
							rb.renderLightOnBlock(i, j, k, 15);
							rb.renderBlockByRenderType(b, i, j, k);
							Tessellator.instance.draw();
						}
					}
				}
			}
			pass++;
			GL11.glPopMatrix();

		}
		RenderHelper.enableStandardItemLighting();
	}
	
	public void setupTranslationAndRotation(Vehicle v, double x, double y, double z) {
		DefaultMotionState motionState = (DefaultMotionState) v.getBody().getMotionState();
		Transform t = motionState.graphicsWorldTrans;
		
		MatrixUtil.getOpenGLSubMatrix(t.basis, glMatrix);
		glMatrix[12] = 0;
		glMatrix[13] = 0;
		glMatrix[14] = 0;
		glMatrix[15] = 1f;
		
		floatBuffer.clear();
		floatBuffer.put(glMatrix).flip();
		GL11.glTranslatef((float) x-0.5f, (float) y-0.5f, (float) z-0.5f);
		GL11.glMultMatrix(floatBuffer); // set vehicle rotation
		
		
	}

	public void setupTransform(Vehicle v) {
		DefaultMotionState motionState = (DefaultMotionState) v.getBody().getMotionState();
		motionState.graphicsWorldTrans.getOpenGLMatrix(glMatrix);
		floatBuffer.clear();
		floatBuffer.put(glMatrix).flip();
		
		
		
//		GL11.glMultMatrix(floatBuffer);
		GL11.glLoadMatrix(floatBuffer);
		
//		System.out.printf("%.4f %.4f %.4f \n", motionState.graphicsWorldTrans.origin.x, motionState.graphicsWorldTrans.origin.y, motionState.graphicsWorldTrans.origin.z);
		
		
	}

	public void setupTranslationAndRotation(Vehicle v, double x, double y, double z, Vec3D corner, float angle, float ticks) {
		GL11.glTranslatef((float) x, (float) y, (float) z);
		System.out.printf("%.4f %.4f %.4f \n", x, y, z);
//		 GL11.glTranslatef(-0.5F, 0F, -0.5F);
//		GL11.glRotatef(90F-angle, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(angle, 0.0F, 1.0F, 0.0F);
		
		float ang1 = (float) v.timeSinceHit - ticks;

		float ang2 = (float) (v.damageTaken - ticks);
		if (ang2 < 0.0F) {
			ang2 = 0.0F;
		}
		if (ang1 > 0.0F) {
			GL11.glRotatef(((MathHelper.sin(ang1) * ang1 * ang2) / 10F) * (float) v.forwardDirection, 1.0F, 0.0F, 0.0F);
		}

	}

}
